package com.ruoyi.system.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.SysMember;
import com.ruoyi.system.domain.SysMemberCar;
import com.ruoyi.system.domain.SysParking;
import com.ruoyi.system.domain.parm.SysMemberCarParm;
import com.ruoyi.system.service.ISysMemberCarService;
import com.ruoyi.system.service.ISysMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.UUID;

/**
 * 会员车牌Controller
 * 
 * @author ruoyi
 * @date 2024-04-07
 */
@Api(tags = "会员车牌")
@RestController
@RequestMapping("/mem/car")
public class MemMemberCarController extends BaseController
{
    @Autowired
    private ISysMemberCarService sysMemberCarService;
    @Autowired
    private ISysMemberService sysMemberService;
    /**
     * 查询会员车牌列表
     */
    @ApiOperation(value = "查询会员车牌列表")
    @GetMapping("/list")
    public TableDataInfo list(SysMemberCar sysMemberCar) throws Exception {
        SysMember user = sysMemberService.getUserInfo();
        if(null == user){
            throw new Exception("用户未登陆");
        }
        sysMemberCar.setMemberId( user.getId());
        startPage();
        List<SysMemberCar> list = sysMemberCarService.selectSysMemberCarList(sysMemberCar);
        return getDataTable(list);
    }


    /**
     * 获取会员车牌详细信息
     */
    @ApiOperation(value = "获取会员车牌详细信息")
    @GetMapping(value = "/info")
    public AjaxResult getInfo(@RequestParam(name = "id",required = true) String  id) throws Exception {
        SysMember user = sysMemberService.getUserInfo();
        if(null == user){
            throw new Exception("用户未登陆");
        }
        return success(sysMemberCarService.selectSysMemberCarById(id));
    }

    /**
     * 新增会员车牌
     */
    @ApiOperation(value = "新增会员车牌")
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody @Valid SysMemberCarParm sysMemberCarParm) throws Exception {
        SysMember user = sysMemberService.getUserInfo();
        if(null == user){
            throw new Exception("用户未登陆");
        }

        SysMemberCar sysMemberCar = new SysMemberCar();
        BeanUtils.copyProperties(sysMemberCarParm, sysMemberCar);
        sysMemberCar.setMemberId(user.getId() );

        // 查询车牌号是否存在
        List<SysMemberCar> isHas = sysMemberCarService.selectSysMemberCarByLicensePlate(sysMemberCar.getLicensePlate());
        if(isHas.size()>0){
            throw new Exception("车牌号已经存");
        }
        sysMemberCar.setId(UUID.randomUUID().toString());
        return toAjax(sysMemberCarService.insertSysMemberCar(sysMemberCar));
    }

    /**
     * 修改会员车牌
     */
    @ApiOperation(value = "修改会员车牌")
    @PutMapping(value = "/edit")
    public AjaxResult edit(@RequestBody @Valid SysMemberCarParm sysMemberCarParm) throws Exception {
        SysMember user = sysMemberService.getUserInfo();
        if(null == user){
            throw new Exception("用户未登陆");
        }

        SysMemberCar sysMemberCar = new SysMemberCar();
        BeanUtils.copyProperties(sysMemberCarParm, sysMemberCar);
        sysMemberCar.setMemberId(user.getId() );

        // 查询车牌号是否存在
        List<SysMemberCar> isHas = sysMemberCarService.selectSysMemberCarByLicensePlate(sysMemberCar.getLicensePlate());
        if(isHas.size()>0){
            for (SysMemberCar item : isHas){
                if(!item.getId().equals(sysMemberCar.getId())){
                    throw new Exception("车牌号已经存");
                }

            }
        }

        return toAjax(sysMemberCarService.updateSysMemberCar(sysMemberCar));
    }

    /**
     * 删除会员车牌
     */
    @ApiOperation(value = "删除会员车牌")
	@DeleteMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) throws Exception {
        SysMember user = sysMemberService.getUserInfo();
        if(null == user){
            throw new Exception("用户未登陆");
        }

        return toAjax(sysMemberCarService.deleteSysMemberCarByIds(ids,user.getId()));
    }
}
